package com.whmskj.xjlhbc.service.impl;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whmskj.xjlhbc.domain.Bcsc06GroundLibraryDataSheet;
import com.whmskj.xjlhbc.domain.Mesjzk;
import com.whmskj.xjlhbc.dto.JzkDto;
import com.whmskj.xjlhbc.mapper.Bcsc06GroundLibraryDataSheetMapper;
import com.whmskj.xjlhbc.mapper.MesjzkMapper;
import com.whmskj.xjlhbc.service.IMesjzkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class MesjzkServiceImpl extends ServiceImpl<MesjzkMapper, Mesjzk> implements IMesjzkService {

    @Autowired
    private Bcsc06GroundLibraryDataSheetMapper mapper;

    @Override
    public JzkDto jzkTj() {
        JzkDto result = new JzkDto();
        List<List<Mesjzk>> list = queryList();
        List<Mesjzk> list1 = list.get(0);
        List<Mesjzk> list2 = list.get(1);
        List<Mesjzk> list3 = list.get(2);
        List<Mesjzk> list4 = list.get(3);
        List<Mesjzk> czList = list1.stream().filter(f -> !f.getZc().equals("空")).collect(Collectors.toList());
        List<Mesjzk> czList1 = list2.stream().filter(f -> !f.getZc().equals("空")).collect(Collectors.toList());
        Set<String> bhList = czList.stream().map(Mesjzk::getBh).collect(Collectors.toSet());
        bhList.addAll(czList1.stream().map(Mesjzk::getBh).collect(Collectors.toSet()));
        bhList.addAll(list3.stream().map(Mesjzk::getBh).collect(Collectors.toSet()));
        bhList.addAll(list4.stream().map(Mesjzk::getBh).collect(Collectors.toSet()));
        long count = czList.size();
        long count2 = czList1.size();
        long count3 = list3.stream().mapToLong(m -> NumberUtil.parseLong(m.getZc(), 0L)).sum();
        long count4 = list4.stream().mapToLong(m -> NumberUtil.parseLong(m.getZc(), 0L)).sum();
        result.setCz(count+count2+count3+count4);
        result.setKz((list1.size() - count) + (list2.size() - count2));
        result.setBh(bhList.size());
        result.setCz01(count);
        result.setKz01(list1.size() - count);
        result.setCz02(count2);
        result.setKz02(list2.size() - count2);
        result.setRk(count3);
        result.setKc(count4);
        result.setPj(count3+count4);
        count = czList.stream().mapToLong(m -> NumberUtil.parseLong(m.getZc(), 0L)).sum();
        count2 = czList1.stream().mapToLong(m -> NumberUtil.parseLong(m.getZc(), 0L)).sum();
        result.setZc(count+count2);
        result.setBhList(bhList.stream().map(bh -> {
            long k1 = czList.stream().filter(f -> f.getBh().equals(bh)).count();
            long k2 = czList1.stream().filter(f -> f.getBh().equals(bh)).count();
            long k3 = list3.stream().filter(f -> f.getBh().equals(bh)).mapToLong(m->NumberUtil.parseLong(m.getZc(),0L)).sum();
            long k4 = list4.stream().filter(f -> f.getBh().equals(bh)).mapToLong(m->NumberUtil.parseLong(m.getZc(),0L)).sum();
            JzkDto.Bh jzkBh = new JzkDto.Bh();
            jzkBh.setBh(bh);
            jzkBh.setK1(k1);
            jzkBh.setK2(k2);
            jzkBh.setK3(k3);
            jzkBh.setK4(k4);
            jzkBh.setZj(k1+k2+k3+k4);
            return jzkBh;
        }).collect(Collectors.toList()));
        result.setZcList(bhList.stream().map(bh -> {
            long k1 = czList.stream().filter(f -> f.getBh().equals(bh)).
                    mapToLong(m -> NumberUtil.parseLong(m.getZc(), 0L)).sum();
            long k2 = czList1.stream().filter(f -> f.getBh().equals(bh)).
                    mapToLong(m -> NumberUtil.parseLong(m.getZc(), 0L)).sum();
            JzkDto.Bh jzkBh = new JzkDto.Bh();
            jzkBh.setBh(bh);
            jzkBh.setK1(k1);
            jzkBh.setK2(k2);
            jzkBh.setZj(k1+k2);
            return jzkBh;
        }).collect(Collectors.toList()));
        return result;
    }

    @Override
    public List<List<Mesjzk>> queryList() {
        List<Mesjzk> list = lambdaQuery().in(Mesjzk::getMacid, "01", "02").list();
        list.forEach(item->{
            boolean match = ReUtil.isMatch(PatternPool.NUMBERS, item.getZc());
            boolean match1 = ReUtil.isMatch(PatternPool.BIRTHDAY, item.getRk());
            item.setZc(match ? item.getZc() : "空");
            item.setBh(match ? item.getBh() : "空");
            item.setRk(match1 ? item.getRk() : null);
            item.setLx("");
        });
        List<Mesjzk> collect = list.stream().filter(f -> f.getMacid().equals("01")).collect(Collectors.toList());
        List<Mesjzk> collect1 = list.stream().filter(f -> f.getMacid().equals("02")).collect(Collectors.toList());
        List<List<Mesjzk>> result = new ArrayList<>();
        result.add(collect);
        result.add(collect1);

        LambdaQueryWrapper<Bcsc06GroundLibraryDataSheet> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Bcsc06GroundLibraryDataSheet::getCheckDate).last("limit 1");
        Bcsc06GroundLibraryDataSheet one = Optional.ofNullable(mapper.selectOne(wrapper)).orElseGet(Bcsc06GroundLibraryDataSheet::new);
        wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Bcsc06GroundLibraryDataSheet::getSpecifications, "190", "230")
                .eq(Bcsc06GroundLibraryDataSheet::getCheckDate, one.getCheckDate());
        List<Bcsc06GroundLibraryDataSheet> bcsc06GroundLibraryDataSheets = mapper.selectList(wrapper);
        List<Mesjzk> collect3 = bcsc06GroundLibraryDataSheets.stream().filter(f -> f.getSpecifications().equals("190")).map(m->{
            Mesjzk jzk = new Mesjzk();
            jzk.setId(m.getId());
            jzk.setMacid(m.getSpecifications());
            jzk.setZwh(m.getId().toString());
            jzk.setZc(m.getNumberOfAxesStored());
            jzk.setBh(m.getBubbleNumber());
            jzk.setRk(m.getCheckDate());
            jzk.setTs(m.getOperationTime());
            return jzk;
        }).collect(Collectors.toList());
        List<Mesjzk> collect4 = bcsc06GroundLibraryDataSheets.stream().filter(f -> f.getSpecifications().equals("230")).map(m->{
            Mesjzk jzk = new Mesjzk();
            jzk.setId(m.getId());
            jzk.setMacid(m.getSpecifications());
            jzk.setZwh(m.getId().toString());
            jzk.setZc(m.getNumberOfAxesStored());
            jzk.setBh(m.getBubbleNumber());
            jzk.setRk(m.getCheckDate());
            jzk.setTs(m.getOperationTime());
            return jzk;
        }).collect(Collectors.toList());
        result.add(collect3);
        result.add(collect4);
        return result;
    }
}
